<template>
  <a-spin :spinning="loading">
    <a-form :form="form" @submit="handleSubmit" ref="formObject">
      <a-form-item v-bind="formItemLayout">
        <span slot="label">
          昵称
        </span>
        <a-input v-decorator="formModel.nickname" />
      </a-form-item>
      <a-form-item v-bind="formItemLayout" label="手机号">
        <a-input v-decorator="formModel.mobile" style="width: 100%">
          <a-select
            slot="addonBefore"
            v-decorator="['prefix', { initialValue: '86' }]"
            style="width: 70px"
          >
            <a-select-option value="86">
              +86
            </a-select-option>
          </a-select>
        </a-input>
      </a-form-item>
      <a-form-item v-bind="formItemLayout" label="备注" help="您对用户的备注">
        <a-input v-decorator="formModel.desc" style="width: 100%"> </a-input>
      </a-form-item>
      <a-form-item v-if="model.id > 0" v-bind="formItemLayout" label="状态">
        <a-switch
          v-decorator="formModel.status"
          checkedChildren="启用"
          unCheckedChildren="禁用"
        ></a-switch>
      </a-form-item>
      <a-form-item v-bind="tailFormItemLayout">
        <a-button type="primary" html-type="submit" :loading="loading">
          保存
        </a-button>
      </a-form-item>
    </a-form>
  </a-spin>
</template>

<script>
import { createUser, updateUser } from "@/api/user";
export default {
  data() {
    return {
      loading: true,
      formModel: {
        nickname: [
          "nickname",
          {
            rules: [
              {
                required: true,
                message: "请输入昵称!",
                whitespace: true
              }
            ]
          }
        ],
        mobile: [
          "mobile",
          {
            rules: [{ required: true, message: "请输入手机号!" }]
          }
        ],
        desc: ["desc", []],
        status: ["status", { valuePropName: "checked" }]
      },
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 8 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 12 }
        }
      },
      tailFormItemLayout: {
        wrapperCol: {
          xs: {
            span: 24,
            offset: 0
          },
          sm: {
            span: 16,
            offset: 8
          }
        }
      }
    };
  },
  props: {
    model: {
      default: ""
    }
  },
  created() {
    if (this.model && this.model.id > 0) {
      this.formModel.nickname[1].initialValue = this.model.nickname;
      this.formModel.mobile[1].initialValue = this.model.mobile;
      this.formModel.desc[1].initialValue = this.model.desc;
      this.formModel.status[1].initialValue = this.model.status ? true : false;
    }
    this.form = this.$form.createForm(this);
    this.loading = false;
  },
  methods: {
    handleSubmit(e) {
      e.preventDefault();
      this.form.validateFieldsAndScroll((err, values) => {
        if (!err) {
          this.loading = true;
          if (this.model && this.model.id > 0) {
            //编辑
            updateUser(values, this.model.id)
              .then(res => {
                this.loading = false;
                this.$emit("success", res);
              })
              .catch(error => {
                this.loading = false;

                this.$common.validateExtend(error, this.form);
              });
          } else {
            //添加
            createUser(values)
              .then(res => {
                this.loading = false;
                this.$emit("success", res);
              })
              .catch(error => {
                this.loading = false;
                this.$common.validateExtend(error, this.form);
              });
          }
        }
      });
    }
  }
};
</script>
